package cn.lblbc.appstore.utils

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.util.Log
import cn.lblbc.appstore.module.download.StatusManager

class PackageReceiver : BroadcastReceiver() {
    private val tag = "PackageReceiver"
    private var mContext: Context? = null
    override fun onReceive(context: Context?, intent: Intent?) {
        mContext = context
        if (intent != null) {
            Log.i(tag, "PackageReceiver - " + intent.action)
            val packageName = intent.data!!.schemeSpecificPart
            if (intent.action.equals(Intent.ACTION_PACKAGE_ADDED)) {
                val replaceIng = intent.getBooleanExtra("android.intent.extra.REPLACING", false)
                Log.i(tag, "add package:$packageName")
                StatusManager.notifyInstallChanges(packageName, false)
            } else if (intent.action.equals(Intent.ACTION_PACKAGE_REMOVED)) {
                val replaceIng2 = intent.getBooleanExtra("android.intent.extra.REPLACING", false)
                Log.i(tag, "remove package:$packageName")
            } else if (intent.action.equals(Intent.ACTION_PACKAGE_REPLACED)) {
                Log.i(tag, "replace package:$packageName")
                StatusManager.notifyInstallChanges(packageName, false)
            }
            StatusManager.checkLocalAppStatus(packageName)
        }
    }
}